package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buba.feign.socialFeign;
import com.buba.mapper.AccountingMapper;
import com.buba.mapper.DepartmentMapper;
import com.buba.mapper.PostMapper;
import entity.salary.Social;
import entity.salary.vo.selAccSocialAccumVo;
import entity.staff.Accounting;

import com.buba.service.AccountingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.staff.Department;
import entity.staff.Post;
import entity.staff.dto.saveAccountDto;
import entity.staff.dto.updateAccSecurityDto;
import entity.staff.dto.withAccumPerDto;
import entity.staff.dto.withSocialPerDto;
import entity.staff.vo.accountingVo;
import entity.staff.vo.selAccVo;
import javafx.geometry.Pos;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import utils.PageResult;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-03-14
 */
@Service
public class AccountingServiceImpl extends ServiceImpl<AccountingMapper, Accounting> implements AccountingService {

    @Resource
    private AccountingMapper accountingMapper;
    @Resource
    private socialFeign socialFeign;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private PostMapper postMapper;
    /* *
     * @Author: beisheng
     * @Descriprion: 根据多个用户id查询用户数据
     * @Date: 2023/3/15 19:57
     * @Return: java.util.List<entity.staff.Accounting>
     * @Param: [splitLiss]
     */

    @Override
    public List<Accounting> selstaffByIds(List<String> splitLiss) {
        List<Accounting> accountings = accountingMapper.selectList(new QueryWrapper<Accounting>().in("oz_id", splitLiss));
        return accountings;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据社保方案id分页模糊查询关联的人员
     * @Date: 2023/3/16 10:56
     * @Return: java.util.List<entity.staff.Accounting>
     * @Param: [socialPerDto]
     */

    @Override
    public PageResult selstaffBySocial(withSocialPerDto socialPerDto) {
        //获取用户数据
        Integer page = socialPerDto.getSocialDto().getPage();
        Integer pageSize = socialPerDto.getSocialDto().getPagesize();
        //模糊查数据
        String mess  = socialPerDto.getMess();
        //社保方案ID
        Integer socialId = socialPerDto.getSocialId();
        //部门ID
        Integer departmentId = socialPerDto.getDepartmentId();
        //员工类型
        String staffType = socialPerDto.getStaffType();
        //分页查询数据
        Page<Accounting> accountingPage = Page.of(page,pageSize);
        QueryWrapper<Accounting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);
        if(socialId!=null){
            queryWrapper.eq("security_id",socialId);
        }else {
            queryWrapper.eq("security_id",0);
        }
        if(mess!=null){
            queryWrapper.eq("b_name",mess).or().eq("number",mess).or().eq("id_number",mess).or().eq("phone",mess);
        }
        if(departmentId!=null){
            queryWrapper.eq("department_id",departmentId);
        }
        if(staffType!=null){
            queryWrapper.eq("employee_status",staffType);
        }
        List<accountingVo> accountingVos = new ArrayList<>();
        //查询数据
        Page<Accounting> accountingPages = accountingMapper.selectPage(accountingPage, queryWrapper);
        List<Accounting> accountings = accountingMapper.selectList(queryWrapper);
        for (Accounting accounting : accountings) {
            accountingVo accountingOne = new accountingVo();
            BeanUtils.copyProperties(accounting,accountingOne);
            if(accounting.getDepartmentId()!=null){
                Department department = departmentMapper.selectById(accounting.getDepartmentId());
                accountingOne.setDepartmentName(department.getDepartName());
            }
            if (accounting.getPostId() != null) {
                Post post = postMapper.selectById(accounting.getPostId());
                accountingOne.setPositionName(post.getPostName());
            }
            accountingVos.add(accountingOne);
        }
        //返回数据
        return new PageResult(page,pageSize,accountings.size(),accountingVos);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 给员工绑定社保方案
     * @Date: 2023/3/17 9:42
     * @Return: common.R
     * @Param: [saveAccountDto]
     */

    @Override
    public void saveAccountToSocial(saveAccountDto saveAccountDto) {
        Accounting accounting = new Accounting();
        accounting.setSecurityId(saveAccountDto.getSocialId());
        accountingMapper.update(accounting,new QueryWrapper<Accounting>().in("oz_id",saveAccountDto.getSocialIds()));
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 批量将用户解绑社保方案
     * @Date: 2023/3/17 13:47
     * @Return: void
     * @Param: [ids]
     */

    @Override
    public void updateACCScheme(List<String> ids) {
        for (String id : ids) {
            Accounting accounting = new Accounting();
            accounting.setSecurityId(0);
            accounting.setUpdateTime(new Date());
            accountingMapper.update(accounting,new QueryWrapper<Accounting>().eq("security_id",id));
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 给员工绑定公积金方案
     * @Date: 2023/3/17 9:42
     * @Return: common.R
     * @Param: [saveAccountDto]
     */

    @Override
    public void saveAccountToAccumulation(saveAccountDto saveAccountDto) {
        Accounting accounting = new Accounting();
        accounting.setAccumulationId(saveAccountDto.getSocialId());
        accountingMapper.update(accounting,new QueryWrapper<Accounting>().in("oz_id",saveAccountDto.getSocialIds()));
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据公积金方案id分页模糊查询关联的人员
     * @Date: 2023/3/16 10:56
     * @Return: java.util.List<entity.staff.Accounting>
     * @Param: [socialPerDto]
     */

    @Override
    public PageResult selstaffByAccum(withAccumPerDto accumPerDto) {
        //获取用户数据
        Integer page = accumPerDto.getSocialDto().getPage();
        Integer pageSize = accumPerDto.getSocialDto().getPagesize();
        //模糊查数据
        String mess  = accumPerDto.getMess();
        //公积金方案ID
        Integer accumId = accumPerDto.getAccumId();
        //部门ID
        Integer departmentId = accumPerDto.getDepartmentId();
        //员工类型
        Integer staffType = accumPerDto.getStaffType();
        //分页查询数据
        Page<Accounting> accountingPage = Page.of(page,pageSize);
        QueryWrapper<Accounting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);
        queryWrapper.eq("accumulation_id",accumId);
        if(mess!=null){
            queryWrapper.eq("b_name",mess).or().eq("number",mess).or().eq("id_number",mess).or().eq("phone",mess);
        }
        if(departmentId!=null){
            queryWrapper.eq("department_id",departmentId);
        }
        if(staffType!=null){
            queryWrapper.eq("employee_status",staffType);
        }
        //查询数据
        Page<Accounting> accountingPages = accountingMapper.selectPage(accountingPage, queryWrapper);
        List<Accounting> accountings = accountingMapper.selectList(queryWrapper);
        //返回数据
        return new PageResult(page,pageSize,accountings.size(),accountingPages.getRecords());
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 给员工绑定公积金方案
     * @Date: 2023/3/17 9:42
     * @Return: common.R
     * @Param: [saveAccountDto]
     */

    @Override
    public void saveAccountToAccum(saveAccountDto saveAccountDto) {
        Accounting accounting = new Accounting();
        accounting.setAccumulationId(saveAccountDto.getSocialId());
        accountingMapper.update(accounting,new QueryWrapper<Accounting>().in("oz_id",saveAccountDto.getSocialIds()));
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 批量将用户解绑公积金方案
     * @Date: 2023/3/17 13:47
     * @Return: void
     * @Param: [ids]
     */

    @Override
    public void updateAccumScheme(List<String> ids) {
        for (String id : ids) {
            Accounting accounting = new Accounting();
            accounting.setAccumulationId(0);
            accounting.setUpdateTime(new Date());
            accountingMapper.update(accounting,new QueryWrapper<Accounting>().eq("accumulation_id",id));
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 再更改方案关联人员的时候给员工解绑或者绑定方案
     * @Date: 2023/3/19 20:28
     * @Return: void
     * @Param: [updateAccSecurityDto]
     */

    @Override
    public void updateAccSecurity(updateAccSecurityDto updateAccSecurityDto) {
        List<Accounting> accountings = accountingMapper.selectList(new QueryWrapper<Accounting>().eq("delete_flag",0).eq("security_id",updateAccSecurityDto.getSecurityId()));
        List<String> list = new ArrayList<>();
            for (String id : updateAccSecurityDto.getIds()) {
                Accounting accounting = new Accounting();
                accounting.setUpdateTime(new Date());
                accounting.setSecurityId(updateAccSecurityDto.getSecurityId());
                accounting.setOzId(Integer.valueOf(id));
                accountingMapper.updateById(accounting);
            }
            for (Accounting accounting : accountings) {
                list.add(accounting.getOzId().toString());
            }
            List<String> ids = updateAccSecurityDto.getIds();
            for (String s : list) {
                int count = 0;
                for (String id : ids) {
                    if(id.equals(s)){
                      count++;
                    }
                }
                if(count == 0){
                    Accounting accounting = new Accounting();
                    accounting.setUpdateTime(new Date());
                    accounting.setSecurityId(0);
                    accounting.setOzId(Integer.valueOf(s));
                    accountingMapper.updateById(accounting);
                }
            }
        }

    @Override
    public void updateAccum(updateAccSecurityDto updateAccSecurityDto) {
        List<Accounting> accountings = accountingMapper.selectList(new QueryWrapper<Accounting>().eq("delete_flag",0).eq("accumulation_id",updateAccSecurityDto.getSecurityId()));
        List<String> list = new ArrayList<>();
        for (String id : updateAccSecurityDto.getIds()) {
            Accounting accounting = new Accounting();
            accounting.setUpdateTime(new Date());
            accounting.setAccumulationId(updateAccSecurityDto.getSecurityId());
            accounting.setOzId(Integer.valueOf(id));
            accountingMapper.updateById(accounting);
        }
        for (Accounting accounting : accountings) {
            list.add(accounting.getOzId().toString());
        }
        List<String> ids = updateAccSecurityDto.getIds();
        for (String s : list) {
            int count = 0;
            for (String id : ids) {
                if(id.equals(s)){
                    count++;
                }
            }
            if(count == 0){
                Accounting accounting = new Accounting();
                accounting.setUpdateTime(new Date());
                accounting.setAccumulationId(0);
                accounting.setOzId(Integer.valueOf(s));
                accountingMapper.updateById(accounting);
            }
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 查询当前社保关联的人员是否绑定参保档案
     * @Date: 2023/3/20 13:36
     * @Return: java.util.List<entity.staff.Accounting>
     * @Param: [saId]
     */

    @Override
    public List<Accounting> selInsurancefile(Integer saId) {
        List<Accounting> accountings = accountingMapper.selectList(new QueryWrapper<Accounting>().eq("delete_flag", 0).eq("security_id", saId));
        return accountings;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 查询当前公积金关联的人员是否绑定参保档案
     * @Date: 2023/3/20 13:36
     * @Return: java.util.List<entity.staff.Accounting>
     * @Param: [saId]
     */

    @Override
    public List<Accounting> selInsurancefiles(Integer saId) {
        List<Accounting> accountings = accountingMapper.selectList(new QueryWrapper<Accounting>().eq("delete_flag", 0).eq("accumulation_id", saId));
        return accountings;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据人员参保状态模糊查分页查询人员信息
     * @Date: 2023/3/20 19:08
     * @Return: common.R
     * @Param: [status]
     */

    @Override
    public PageResult selAccountBystatus(Map<String,Object> map) {
        List<accountingVo> accountingVos = new ArrayList<>();
        Integer page = (Integer) map.get("page");
        Integer pageNum = (Integer) map.get("pageNum");
        Integer status = (Integer) map.get("status");
        Page<Accounting> accountingPage = new Page<>(page,pageNum);
        QueryWrapper<Accounting> queryWrapper = new QueryWrapper<>();
        if(status!=null){
            queryWrapper.eq("Insurance_status",status);
        }
        queryWrapper.eq("delete_flag",0);
        Page<Accounting> accountingPages = accountingMapper.selectPage(accountingPage, queryWrapper);
        List<Accounting> accountings = accountingMapper.selectList(queryWrapper);
        for (Accounting record : accountingPages.getRecords()) {
            accountingVo accountingVo = new accountingVo();
            BeanUtils.copyProperties(record,accountingVo);
            Department department = departmentMapper.selectOne(new QueryWrapper<Department>().eq("delete_flag", 0).eq("oz_id", record.getDepartmentId()));
            Post post = postMapper.selectOne(new QueryWrapper<Post>().eq("delete_flag", 0).eq("oz_id", record.getPostId()));
            accountingVo.setDepartmentName(department.getDepartName());
            accountingVo.setPositionName(post.getPostName());
            accountingVos.add(accountingVo);
        }
        return new PageResult(page,pageNum,accountings.size(),accountingVos);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 分页查询参保人员信息以及人员关联社保公积金基本信息
     * @Date: 2023/3/21 16:37
     * @Return: common.R
     * @Param: [map]
     */

    @Override
    public PageResult selAccAndSocialAndAccum(Map<String, Integer> map) {
        List<selAccVo> selAccVoList = new ArrayList<>();
        selAccVo selAccVo = new selAccVo();
        Integer page = map.get("page");
        Integer pageNum = map.get("pageNum");
        Integer accStatus = map.get("accStatus");
        Page<Accounting> accountingPage = new Page<>(page,pageNum);
        QueryWrapper<Accounting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0).eq("active_status",accStatus);
        Page<Accounting> accountingPages = accountingMapper.selectPage(accountingPage, queryWrapper);
        List<Accounting> accountings = accountingMapper.selectList(queryWrapper);
        for (Accounting record : accountingPages.getRecords()) {
            BeanUtils.copyProperties(record,selAccVo);
            selAccSocialAccumVo selAccSocialAccumVo = socialFeign.selSocialAndAccum(record.getInsurancefileId());
            selAccVo.setSelAccSocialAccumVo(selAccSocialAccumVo);
            selAccVoList.add(selAccVo);
        }
        return new PageResult(page,pageNum,accountings.size(),selAccVoList);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 办理减员,停保
     * @Date: 2023/3/21 20:05
     * @Return: common.R
     * @Param: [accId]
     */

    @Override
    public void Suspension(Integer accId) {
        Accounting accounting = new Accounting();
        accounting.setSecurityId(0);
        accounting.setAccumulationId(0);
        accounting.setInsurancefileId(0);
        accounting.setInsuranceStatus(2);
        accountingMapper.update(accounting,new QueryWrapper<Accounting>().eq("oz_id",accId).eq("delete_flag",0));
    }

    @Override
    public List<Accounting> selAllPeopleHasNoSocial(List<Integer> ids) {
        return accountingMapper.selectList(new QueryWrapper<Accounting>().eq("delete_flag",0).in("oz_id",ids));
    }

    @Override
    public List<accountingVo> selAllAccountBystatus() {
        List<accountingVo> accountingVos = new ArrayList<>();
        List<Accounting> accountings = accountingMapper.selectList(new QueryWrapper<Accounting>().eq("delete_flag",0));
        for (Accounting record : accountings) {
            accountingVo accountingVo = new accountingVo();
            BeanUtils.copyProperties(record,accountingVo);
            Department department = departmentMapper.selectOne(new QueryWrapper<Department>().eq("delete_flag", 0).eq("oz_id", record.getDepartmentId()));
            Post post = postMapper.selectOne(new QueryWrapper<Post>().eq("delete_flag", 0).eq("oz_id", record.getPostId()));
            accountingVo.setDepartmentName(department.getDepartName());
            accountingVo.setPositionName(post.getPostName());
            accountingVos.add(accountingVo);
        }
        return accountingVos;
    }

}
